var prefix = jsctx + "/tenantOperator";

var operatorElecStation = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageOperatorElecStation",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    cache: false,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            tenantOperatorCode: $('#tenantOperatorCode').val()
                        };
                    },
                    columns: [
                        {
                            checkbox: true, field: '', width: '50', align: 'left',
                        },
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'operatorName', title: '分销商名称', align: 'left', width: '100'},
                        {field: 'loginName', title: '账号', width: '100', align: 'left'},
                        {field: 'stationName', title: '桩群名称', width: '100', align: 'left'},

                        {
                            field: 'govtHlhtType', title: '是否有申报资格',
                            width: '100', align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span class="label label-primary">否</span>'
                                } else if (value == '1') {
                                    return '<span class="label label-success">是</span>'
                                }
                            }
                        },
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    //批量更改状态(开启分销商政府申报)
    function batchTurnOn() {
        var rows = $("#exampleTable").bootstrapTable('getSelections');// 获得要删除的数据
        if (rows.length == 0) {// rows 主要是为了判断是否选中，下面的else内容才是主要
            alert("请先选择申报站点!");
            return;
        } else {
            var operatorStationIdList = new Array();// 声明一个数组
            $(rows).each(function () {// 通过获得别选中的来进行遍历
                operatorStationIdList.push(this.operatorStationId); // cid为获得到的整条数据中的一列
            });
            updateTurnOn(operatorStationIdList)
        }
    }

    function updateTurnOn(operatorStationIdList) {
        layer.confirm('确定要强制失效？', {
            title: '站点申报', content: '设置所勾选站点为可申报站点(有站点申报资格的分销商,在政务类平台对接时可单独申报)', btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/updatebatchType",
                type: "post",
                traditional: true,
                data: {
                    operatorStationIdList: operatorStationIdList,
                    govtHlhtType: "1",
                    tenantOperatorCode: $("#tenantOperatorCode").val()
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("开启成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    //批量更改状态(关闭分销商政府申报)
    function batchShutDown() {
        var rows = $("#exampleTable").bootstrapTable('getSelections');// 获得要删除的数据
        if (rows.length == 0) {// rows 主要是为了判断是否选中，下面的else内容才是主要
            alert("请先选择取消申报站点!");
            return;
        } else {
            var operatorStationIdList = new Array();// 声明一个数组
            $(rows).each(function () {// 通过获得别选中的来进行遍历
                operatorStationIdList.push(this.operatorStationId); // cid为获得到的整条数据中的一列
            });
            updateShutDown(operatorStationIdList)
        }
    }

    function updateShutDown(operatorStationIdList) {
        layer.confirm('确定要取消站点申报？', {
            title: '取消站点申报', content: '取消设置所勾选站点为可申报站点(有站点申报资格的分销商,在政务类平台对接时可单独申报)', btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/updatebatchType",
                type: "post",
                traditional: true,
                data: {
                    operatorStationIdList: operatorStationIdList,
                    govtHlhtType: "0",
                    tenantOperatorCode: $("#tenantOperatorCode").val()
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("取消成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    return {
        init: init,
        reLoad: reLoad,
        batchTurnOn: batchTurnOn,
        batchShutDown: batchShutDown
    };
})();
